package sm.codec.type;

import java.util.ArrayList;
import java.util.List;

import sm.util.Util;

public class Struct implements Cloneable {
	private List<Object> value;

	private int type;

	public Struct(List<Object> value, int type) {
		this.value = value;
		this.type = type;
	}

	public List<Object> getValue() {
		return value;
	}

	public int getType() {
		return type;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + type;
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Struct other = (Struct) obj;
		if (type != other.type)
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}

	@Override
	public Object clone() {
		try {
			Struct v = (Struct) super.clone();
			v.type = type;

			if (value != null) {
				v.value = new ArrayList<Object>();
				for (Object fieldValue : value) {
					v.value.add(Util.cloneData(fieldValue));
				}
			}
			return v;
		} catch (CloneNotSupportedException e) {
			// this shouldn't happen, since we are Cloneable
			throw new InternalError();
		}
	}
}
